package wangwenjun.phase2.concurrencypattern.pipeline;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import util.ThreadUtil;

import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.IntStream;

/**
 * @author ChangLiang
 * @date 2020/6/17
 */
public class AppTest {

    @Test
    @DisplayName("test")
    public void test() throws InterruptedException {
        final ProductPipeline pipeline = new ProductPipeline(5);
        AtomicInteger productId = new AtomicInteger(1);
        // 流水线上有8 个工作人员往传送带上不断地放置等待加工的半成品
        IntStream.range(0,8).forEach(i->new Thread(()->{
            while (true) {
                pipeline.offer(new Product(productId.getAndIncrement()));
                ThreadUtil.sleepForSeconds(ThreadLocalRandom.current().nextInt(10));
            }
        },"producer-thread-"+i).start());
        Thread.currentThread().join();
    }
}
